/*
  Name: DeadProyClientManger.cpp
  Copyright:
  Author: Carl Chatfield
  Date: 06/01/08 12:32
  Description: Used for storing and retrieving information about clients running through the proxy
*/
#include "DeadProxyClientManager.h"
#include <wx/arrimpl.cpp> //this is needed for wxArrays, i dont know why lol
WX_DEFINE_OBJARRAY(ArrayOfClients);

ClientManager::ClientManager()
{
    Clients = new ArrayOfClients;
    mutex = new wxMutex();
}

int ClientManager::GetCount()
{
    return Clients->GetCount();
}

Client * ClientManager::GetClientByPos(unsigned int pos)
{
    mutex->Lock();
    Client * ret;
    if(Clients->GetCount() > pos)
    {
        ret = &Clients->Item(pos);
        mutex->Unlock();
        return ret;
    }
    mutex->Unlock();
    return 0;
}

int ClientManager::GetPosByCharName(wxString name)
{
    int i = 0;
    int len = Clients->GetCount();
    mutex->Lock();
    for(i = 0; i < len; i++)
    {
        if(Clients->Item(i).GetCharName().CmpNoCase(name) == 0)
        {
            mutex->Unlock();
            return i;
        }
    }
    mutex->Unlock();
    return -1;
}

int ClientManager::GetPosByPid(int pid)
{
    int i = 0;
    int len = Clients->GetCount();
    mutex->Lock();
    for(i = 0; i < len; i++)
    {
        if(Clients->Item(i).GetPid() == pid)
        {
            mutex->Unlock();
            return i;
        }
    }
    mutex->Unlock();
    return -1;
}

char * ClientManager::GetKey(int pos)
{
    return GetClientByPos(pos)->GetKey();
}
    

void ClientManager::RemoveClient(int pos)
{
    mutex->Lock();
    ClientEvent * destroyevt = new ClientEvent(CE_DESTROYSELF);
    Clients->Item(pos).PostEvent(destroyevt);
    Clients->Detach((unsigned int)pos);
    mutex->Unlock();
}

void ClientManager::AddClient(int pid, void* handle, bool isStandAlone)
{
    Client * NewClient = new Client(pid, handle, 0, wxT("unknown"), isStandAlone);
    mutex->Lock();
    Clients->Add(NewClient);
    mutex->Unlock();
}

int ClientManager::GetClientWithConnectStatus(int status, int * i)
{
    int nRet = 0;
    int len = Clients->GetCount();
    mutex->Lock();
    for(int ii = 0; ii < len; ii++)
    {
        if(Clients->Item(ii).GetConnectionStatus() == status)
        {
            *i = ii;
            nRet++;
        }
    }
    mutex->Unlock();
    return nRet;
}

void ClientManager::GetConnectingCharacterInfo(int i, int * ip, short * port)
{
    mutex->Lock();
    Clients->Item(i).GetConnectingCharacterInfo(ip, port);
    mutex->Unlock();
}

//void ClientManager::SetLogPacketMode(int i, int mode)
//{
//    mutex->Lock();
//    Clients->Item(i).SetLogPacketMode(mode);
//    mutex->Unlock();
//}
//
//int ClientManager::GetLogPacketMode(int i)
//{
//    int ret;
//    mutex->Lock();
//    ret = Clients->Item(i).GetLogPacketMode();
//    mutex->Unlock();
//    return ret;
//}

//wxString * ClientManager::GetPacketString(int i, int n)
//{
//    wxString * Ret;
//    mutex->Lock();
//    Ret = Clients->Item(i).GetPacketString(n);
//    mutex->Unlock();
//    return Ret;
//}

//void ClientManager::ClearPacketLog(int i)
//{
//    mutex->Lock();
//    Clients->Item(i).ClearPacketLog();
//    mutex->Unlock();
//}
//
//wxArrayString * ClientManager::GetListBoxArray(int i)
//{
//    wxArrayString * Ret;
//    mutex->Lock();
//    Ret = Clients->Item(i).GetListBoxArray();
//    mutex->Unlock();
//    return Ret;
//}

//void ClientManager::SetSelected(int i)
//{
//    mutex->Lock();
//    int n = Clients->GetCount();
//    for(int ii = 0; ii < n; ii++)
//    {
//        Clients->Item(ii).SetSelected(false);
//    }
//    Clients->Item(i).SetSelected(true);
//    mutex->Unlock();
//}

void ClientManager::PostEventToClient(int i, ClientEvent* evt)
{
    mutex->Lock();
    Clients->Item(i).PostEvent(evt);
    mutex->Unlock();
}

void ClientManager::BroadcastEvent(ClientEvent * evt)
{
    mutex->Lock();
    int n = Clients->GetCount();
    for(int i = 0; i < n; i++)
    {
        Clients->Item(i).PostEvent(evt->CloneEvent());
    }
    mutex->Unlock();
    delete evt;
}
